package com.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Controller;

import com.base.BaseAction;
import com.domain.Staff;
import com.service.StaffService;
import com.util.PageBean;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
@Controller("staffAction")
public class StaffAction extends BaseAction<Staff> {
	@Resource(name="staffService")
	private StaffService staffService;
	
	public String save(){					//添加派件员,保存数据
		staffService.save(model);
		return SUCCESS;
	}	
	
	
	//要实现分页
	private int page;
	private int rows;	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}

	public String findAllStaff() throws IOException{			//查询所有派件员
		PageBean pageBean = new PageBean();
		pageBean.setCurrentPage(page);		//当前页
		pageBean.setPageSize(rows);			//每页显示条数
		//使用离线条件查询对象包装查询条件
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Staff.class);
		pageBean.setDetachedCriteria(detachedCriteria);
		pageBean = staffService.findAllStaff(pageBean);
		//讲pagebean转换为json数据返回给前端页面.
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(new String[] {"detachedCriteria","pageSize","currentPage","decidedzones"});
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jsonConfig);
		String json = jsonObject.toString();
		//将json数据通过输出流写到客户端
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(json);
		return NONE;
	}
	
	public String deleteStaff(){									//删除派件员
		String ids = ServletActionContext.getRequest().getParameter("ids");
		staffService.deleteStaff(ids);
		return "success";
	}
	
	public String editStaff(){								//修改派件员
		staffService.updateStaff(model);
		return SUCCESS;
	}
}
